package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.ClazzMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.ClazzQueryParam;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageResult;
import com.itheima.service.ClazzService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ClazzServiceImpl extends ServiceImpl<ClazzMapper,Clazz> implements ClazzService {
    @Autowired
    private EmpService empService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public PageResult<Clazz> page(ClazzQueryParam param) {
        Page<Clazz> page = Page.of(param.getPage(), param.getPageSize());
        page.addOrder(OrderItem.desc("update_time"));
        //构建查询条件
        LambdaQueryWrapper<Clazz> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(param.getName() != null && !param.getName().isEmpty(),Clazz::getName,param.getName())
                .between(param.getBegin() != null && param.getEnd() != null,Clazz::getBeginDate,param.getBegin(),param.getEnd());

        page = page(page,queryWrapper);


        //获取班级集合
        List<Clazz> clazzs = page.getRecords();
        List<Long> masterIds = clazzs.stream().map(clazz->clazz.getMasterId()).collect(Collectors.toList());
        List<Emp> emps = empService.listByIds(masterIds);

        emps.forEach(emp->{
            clazzs.forEach(clazz->{
                if(clazz.getMasterId().equals(emp.getId())){
                    clazz.setMasterName(emp.getName());
                }
            });
        });
        clazzs.forEach(clazz->{
            clazz.setStatus(clazz.getEndDate().isAfter(LocalDate.now())?"在读":"已结课");
            //clazz.setStatus(clazz.getBeginDate().isAfter(LocalDate.now())?"未开班":"在读");
        });
        return new PageResult<>(page.getTotal(), clazzs);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveClazz(Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        save(clazz);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Clazz getById(Long id) {
        Clazz clazz = getById(id);
        return clazz;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        updateById(clazz);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeById(Long id) {
        removeById(id);
    }

}
